<?php

require_once 'UC_DatabaseCollection.php';
require_once 'UC_DBObjectPath.php';
require_once 'UC_Database.php';
require_once 'UC_DBException.php';

class UC_DBServer
{
    public function __construct(PDO $PDO)
    {
        if(!is_null($PDO))
        {
            $this->set_PDO($PDO);
            $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            
            if(! $this->Databases instanceof UC_DatabaseCollection)
            {
                $this->Databases = new UC_DatabaseCollection();
            }
            $this->PopulateDatabases();            
        }
        else 
        {
            throw new InvalidArgumentException('Invalid PDO object.');
        }
    }
    
    public function Delete($object_path)
    {
        
    }
    
    public function ExecuteMethod(
        $object_path, $method_name, $input_parameters = null)
    {
        
    }
    
    public function ExecuteQuery($query_string)
    {
        
    }
    
    public function Get($object_path)
    {
        
    }
    
    public function InstancesOf($object_path)
    {
        
    }
    
    public function get_PDO()
    {
        return $this->PDO;
    }

    public function set_PDO(PDO $PDO)
    {
        $this->PDO = $PDO;
    }

    public function get_Databases()
    {
        return $this->Databases;
    }

    public function set_Databases(UC_DatabaseCollection $Databases)
    {
        $this->Databases = $Databases;
    }

    public function get_CharacterSets()
    {
        if(is_null($this->CharacterSets))
        {
            $sql = 'SELECT CHARACTER_SET_NAME ' .
                'FROM INFORMATION_SCHEMA.CHARACTER_SETS';
            
            try
            {
                $this->CharacterSets = 
                    $this->PDO->query($sql)->fetchAll(PDO::FETCH_COLUMN, 0);
            }
            catch (PDOException $e)
            {
                throw new UC_DBException(
                    'UNable to enumerate character sets.');
            }
        }
        return $this->CharacterSets;
    }

    public function get_Collations()
    {
        if(is_null($this->Collations))
        {
            $sql = 'SELECT COLLATION_NAME ' .
                'FROM INFORMATION_SCHEMA.COLLATIONS';
            
            try
            {
                $this->Collations = 
                    $this->PDO->query($sql)->fetchAll(PDO::FETCH_COLUMN, 0);
            }
            catch (PDOException $e)
            {
                throw new UC_DBException(
                    'Unable to enumerate collations.');
            }
        }
        
        return $this->Collations;
    }

    private function PopulateDatabases()
    {
        $sql = 'Select SCHEMA_NAME from INFORMATION_SCHEMA.SCHEMATA';
        
        try
        {
            $dbs = $this->PDO->query($sql);
        }
        catch (PDOException $e)
        {
            throw new UC_DBException('Unable to enumerate databases.');
        }
        
        foreach($dbs as $db)
        {
            $path = new UC_DBObjectPath();
            $path->set_is_Database(true);
            $path->set_Name($db['SCHEMA_NAME']);
            $path->set_Schema($db['SCHEMA_NAME']);
            $path->set_Server($this);
            
            $database = UC_Database::Get_($path);
            $this->Databases->add($database);
        }
    }
    
    private $PDO;    
    private $Databases;
    private $CharacterSets = null;
    private $Collations = null;
}

?>
